<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../day31/日期.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        a {
            color: inherit;
            text-decoration: none;
        }

        .commentBar {
            width: 600px;
            margin: 0 auto;
        }

        .commentBar .commentSendBox {
            margin-top: 50px;
        }

        .commentBar .commentSendBox textarea {
            width: 100%;
            height: 200px;
            resize: none;
            padding: 10px;
            box-sizing: border-box;
            outline: none;
        }

        .commentBar .commentSendBox .sendBox {
            display: flex;
            justify-content: space-between;
        }

        .commentListBox {
            margin-top: 50px;
            font-size: 12px;
            line-height: 18px;
        }

        .showComment li {
            padding: 10px;
        }

        .showComment li+li {
            border-top: 1px dashed #333;
        }

        .comment {
            text-indent: 10px;
        }

        .commentDetail {
            display: flex;
            text-indent: 10px;
        }

        .reply {
            margin-left: 50px;
        }

        .replyList {
            padding-left: 50px;
        }

        .replyList .comment-header {
            margin-top: 12px;
        }

        .comment-bottom {
            height: 80px;
            display: flex;
            margin-top: 20px;
            display: none;
        }

        .comment-bottom .replyCon {
            flex: 1;
            height: 80px;
            border-radius: 10px;
            margin-right: 10px;
            outline: none;
            padding: 10px;
            box-sizing: border-box;
            resize: none;
        }

        .comment-bottom .replyBtn {
            width: 80px;
            height: 80px;
            background-color: #00b5e5;
            border-radius: 10px;
            word-break: break-all;
            font-size: 24px;
            word-spacing: 10px;
            line-height: 40px;
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="commentBar">
        <div class="commentSendBox">
            <textarea name="" id="" cols="30" rows="10" class="commentCon"></textarea>
            <div class="sendBox">
                <div class="commentTips">您还可以输入50个字</div>
                <button class="commentSend">发送</button>
            </div>
        </div>
        <div class="commentListBox">
            <ul class="showComment">
            </ul>
        </div>
    </div>
</body>
<script>
    var commentCon = document.getElementsByClassName("commentCon")[0];
    var commentTips = document.getElementsByClassName("commentTips")[0];
    var commentSend = document.getElementsByClassName("commentSend")[0];
    var showComment = document.getElementsByClassName("showComment")[0];

    var replyList = document.getElementsByClassName("reply");
    var replySubList = document.getElementsByClassName("replyList");
    var commentBottomList = document.getElementsByClassName("comment-bottom");
    var replyConList = document.getElementsByClassName("replyCon");
    var replyBtnList = document.getElementsByClassName("replyBtn");


    commentCon.onkeypress = function (e){
        var e = e || window.event;
        var keyCode = e.which || e.keyCode;
        if(keyCode == 10){
            commentSendhandler();
        }
    }

    commentSend.onclick = commentSendhandler;

    var keyWordList = ["菜B" , "有病" , "CNM" , "MD"]

    function commentSendhandler(){
        var comment = commentCon.value.trim();//trim()去除空格
        if(comment){
            // 对敏感词过滤(替换*)
            var reg = new RegExp(keyWordList.join("|") , "ig");
            comment = comment.replace(reg , "*")

            var li = document.createElement("li");
            li.innerHTML = ` <div class="comment-header">
                        <div class="nick">PigZhang</div>
                        <div class="comment">${comment}</div>
                        <div class="commentDetail">
                            <div class="time">${dateFormat("YY-MM-DD hh:mm")}</div>
                            <div class="reply">回复</div>
                        </div>
                    </div>
                    <div class="replyList">
                       
                    </div>
                    <div class="comment-bottom">
                        <textarea class="replyCon" name="" id="" cols="30" rows="10"></textarea>
                        <a class="replyBtn" href="javascript:;">发表 评论</a>
                    </div>`
        }
        showComment.prepend(li);
        commentCon.value = "";
    }

    showComment.onclick = function(e){
        var e = e || window.event;
        var target = e.target || e.srcElement;
        console.log(target);
        if(target.className == "reply"){
            console.log("1");
            var index = Array.from(replyList).indexOf(target);
            for(var i = 0 ; i < commentBottomList.length ; i++){
                commentBottomList[i].style.display = "flex";
            }
        }else if(target.className == "replyBtn"){
            var index = Array.from(replyBtnList).indexOf(target);
            var subComment = replyConList[index].value.trim();

            var reg = new RegExp(keyWordList.join("|") , "ig");
            subComment = subComment.replace(reg , "*")

            var div = document.createElement("div");
            div.className = "comment-header";
            div.innerHTML = `<div class="comment-header">
                            <div class="nick">PigZhang: <span class="comment">${subComment}</span></div>
                            <div class="commentDetail">
                                <div class="time">${dateFormat("YY-MM-DD hh:mm")}</div>
                            </div>
                        </div>`
            
            replySubList[index].prepend(div);
            replyConList[index].value = "";
            commentBottomList[index].style.display = "none";
        }
    }
</script>

</html>